package com.awspaas.user.apps.sts.util;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:Lizj
 * @create: 2022-10-15 23:37
 * @Description: 操作中间操作日志表工具类
 */
public class MiddleLogUtil {
    /**
     * @methodName: 查询不为某按钮的按钮集合
     * @param:
     * @return:
     * @description:
     * @auther: Lizj
     * @date: 2022/10/20 0:15
     */
    public List<String> getSignBtnLogButtons(Map<String,String> paramsMap, String bpmButtonId) {
        String processDefId = paramsMap.get("PROCESSDEFID");
        String processInstId = paramsMap.get("PROCESSINSTID");
        String taskInstId = paramsMap.get("TASKINSTID");
        List<String> buttonList = DBSql.getList("SELECT DISTINCT BPMBUTTONNAME FROM BO_PS_MIDDLE_OPERATION_LOG where PROCESS_INSTID = ? AND PROCESS_INSTID = ? AND TASK_INSTID = ? AND  BPMBUTTONNAME != ?", String.class, new Object[]{processDefId, processInstId, taskInstId, bpmButtonId});
        return buttonList;
    }
    /**
     * @methodName: 获取指定条件查询按钮最新日志记录
     * @param:
     * @return:
     * @description:
     * @auther: Lizj
     * @date: 2022/10/20 0:02
     */
    public RowMap getSignBtnLogMax(Map<String,String> paramsMap, String bpmButtonId) {
        System.out.println("按钮名称："+bpmButtonId);
        System.out.println("入参12："+paramsMap);
        String processDefId = paramsMap.get("PROCESSDEFID");
        String processInstId = paramsMap.get("PROCESSINSTID");
        String taskInstId = paramsMap.get("TASKINSTID");
        RowMap signMaxLog = DBSql.getMap("SELECT loga.* FROM BO_PS_MIDDLE_OPERATION_LOG loga WHERE loga.PROCESS_DEFID = ? AND loga.PROCESS_INSTID = ? AND " +
                        "loga.TASK_INSTID = ? AND loga.BPMBUTTONNAME = ? AND RECEIVERESULTS = (SELECT MAX(RECEIVERESULTS) FROM BO_PS_MIDDLE_OPERATION_LOG logb " +
                        "WHERE loga.PROCESS_DEFID=logb.PROCESS_DEFID AND loga.PROCESS_INSTID=logb.PROCESS_INSTID AND loga.TASK_INSTID=logb.TASK_INSTID AND loga.BPMBUTTONNAME=logb.BPMBUTTONNAME)",
                new Object[]{processDefId, processInstId, taskInstId, bpmButtonId});
        System.out.println(signMaxLog);
        return signMaxLog;
    }

    /**
     * @methodName: 插入中间操作日志表
     * @param:
     * @return:
     * @description:
     * @auther: Lizj
     * @date: 2022/10/16 0:22
     */
    public String InsertBtnLog(Map<String,String> paramsMap) {
        System.out.println("插入参数："+paramsMap);
        String operation = "";
        String sid = paramsMap.get("sid");
        UserContext uc = null;
        if (null!=sid && !sid.equals("")) {
            uc = UserContext.fromSessionId(sid);
        } else {
            uc = UserContext.fromUID("admin");
        }
        RowMap otherButtonRowMap = DBSql.getMap("SELECT * FROM BO_PS_BUSINESS_BUTTON_REGISTER WHERE BPMBUTTONNAME = ? AND " +
                "PROCESS_DEFID = ? AND ACTIVITY_DEFID = ?", new Object[]{paramsMap.get("BPMBUTTONID"),
                paramsMap.get("PROCESSDEFID"), SDK.getTaskAPI().getInstanceById(paramsMap.get("TASKINSTID")).getActivityDefId()});
        // System.out.println("按钮url++++++++++++++++++++++++++++++++++++++++++++++"+otherButtonRowMap.getString("APPBUTTONURL"));
        BO bo = new BO();
        String applicationname = paramsMap.get("APPLICATIONNAME");
        String processInstID = paramsMap.get("PROCESSINSTID");
        bo.set("APPLICATIONNAME", applicationname);
        bo.set("TASK_INSTID", paramsMap.get("TASKINSTID"));
        bo.set("PROCESS_INSTID", paramsMap.get("PROCESSINSTID"));
        bo.set("PROCESS_DEFID", paramsMap.get("PROCESSDEFID"));
        bo.set("BUSINESSID", paramsMap.get("BUSINESSID"));
        bo.set("BUSINESSNAME", paramsMap.get("BUSINESSNAME"));
        bo.set("BUSINESSSTATUSID", paramsMap.get("BUSINESSSTATUSID"));
        bo.set("BUSINESSSTATUSNAME", paramsMap.get("BUSINESSSTATUSNAME"));
        bo.set("BUTTONID", otherButtonRowMap.getString("APPBUTTONID"));
        bo.set("BUTTONNAME", otherButtonRowMap.getString("APPBUTTONNAME"));
        bo.set("BUTTONURL", otherButtonRowMap.getString("APPBUTTONURL"));
        bo.set("BUTTONTYPE", paramsMap.get("BUTTONTYPE"));
        bo.set("BPMBUTTONID", paramsMap.get("BPMBUTTONID"));
        bo.set("BPMBUTTONNAME", paramsMap.get("BPMBUTTONID"));
        bo.set("REJECTIONTYPE", paramsMap.get("REJECTIONTYPE"));
        bo.set("SENDREFERENCE", paramsMap.get("SENDREFERENCE"));
        bo.set("PARTICIPATION", paramsMap.get("PARTICIPATION"));
        bo.set("BUTTONSTATUS", paramsMap.get("PARTICIPATION"));
        bo.set("BUSINESSRESULT", paramsMap.get("BUSINESSRESULT"));
        bo.set("BUSINESSSTATUSTIME", paramsMap.get("BUSINESSSTATUSTIME"));
        bo.set("NOTES", paramsMap.get("NOTES"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long nowTimeStamp = System.currentTimeMillis();
        Date nowDateTime = new Date(nowTimeStamp);
        String nowDateTimeFormat = sdf.format(nowDateTime);
        String createlogtimestamp = paramsMap.get("CREATELOGTIMESTAMP");
        bo.set("CREATELOGDATETIME", nowDateTimeFormat);
        bo.set("RECEIVERESULTS", nowDateTimeFormat);
        if (null!=createlogtimestamp && !createlogtimestamp.equals("")) {
            bo.set("CREATELOGTIMESTAMP", createlogtimestamp);
        } else {
            bo.set("CREATELOGTIMESTAMP", nowTimeStamp);
        }
        int recordCount = 0;
        try {
            //获取创建人信息
//            String processInstanceId = processInstance.getId();
            ProcessInstance processInstance = SDK.getProcessAPI().createBOProcessInstance("obj_9a90f21529e2415ca07dff0b3cbe9efc", "admin", "统一操作日志表"+"系统标识："+applicationname);

            SDK.getBOAPI().create("BO_PS_MIDDLE_OPERATION_LOG",bo,processInstance.getId(),"admin");
            SDK.getLogAPI().consoleInfo("====================创建完成");
//            recordCount = SDK.getBOAPI().createDataBO("BO_PS_MIDDLE_OPERATION_LOG", bo, uc);
            operation = "insertOK";
        } catch (Exception e) {
            operation = "insertERROR";
        }
        return operation;
    }
    /**
     * @methodName: 更新中间操作日志表
     * @param:
     * @return:
     * @description:
     * @auther: Lizj
     * @date: 2022/10/16 0:24
     */
    public String UpdateBtnLog(Map<String,String> paramsMap) {
        System.out.println("入参：+++" + paramsMap);
        String operation = "";
        String processDefId = paramsMap.get("PROCESSDEFID");
        String processInstId = paramsMap.get("PROCESSINSTID");
        String taskInstId = paramsMap.get("TASKINSTID");
        String bpmbuttonid = paramsMap.get("BPMBUTTONID");
        String createlogtimestamp = paramsMap.get("CREATELOGDATETIME");


        //查询日志表数据
        List<BO> logBos = SDK.getBOAPI()
                .query("BO_PS_MIDDLE_OPERATION_LOG", true)
                .addQuery("PROCESS_DEFID = ", processDefId)
                .addQuery("PROCESS_INSTID = ", processInstId)
                .addQuery("TASK_INSTID = ", taskInstId)
                .addQuery("BPMBUTTONNAME = ", bpmbuttonid)
                .orderBy("CREATELOGTIMESTAMP").desc().list();

        /*RowMap signMaxLog = DBSql.getMap("SELECT loga.* FROM BO_PS_MIDDLE_OPERATION_LOG loga WHERE loga.PROCESS_DEFID = ? AND loga.PROCESS_INSTID = ? AND " +
                        "loga.TASK_INSTID = ? AND loga.BPMBUTTONID = ? AND RECEIVERESULTS = (SELECT MAX(RECEIVERESULTS) FROM BO_PS_MIDDLE_OPERATION_LOG logb " +
                        "WHERE loga.PROCESS_DEFID=logb.PROCESS_DEFID AND loga.PROCESS_INSTID=logb.PROCESS_INSTID AND loga.TASK_INSTID=logb.TASK_INSTID AND loga.BPMBUTTONID=logb.BPMBUTTONID)",
                new Object[]{processDefId, processInstId, taskInstId, bpmButtonId});*/
        //若统一中间操作日志表(BO_PS_MIDDLE_OPERATION_LOG)不为空--有数据
        if (null!=logBos && !logBos.isEmpty()) {
            System.out.println("查询到的结果："+logBos);
            BO signMaxLog = logBos.get(0);
            String buttonstatus = signMaxLog.getString("BUTTONSTATUS");
            System.out.println("查看到的按钮状态为：" + buttonstatus + "--------------------------");
            if (null==buttonstatus && buttonstatus.equals("")) {
                MiddleLogUtil middleLogUtil = new MiddleLogUtil();
                paramsMap.put("BUSINESSID", signMaxLog.getString("BUSINESSID"));
                paramsMap.put("BUSINESSNAME", signMaxLog.getString("BUSINESSNAME"));
                paramsMap.put("BUSINESSSTATUSID", signMaxLog.getString("BUSINESSSTATUSID"));
                paramsMap.put("BUSINESSSTATUSNAME", signMaxLog.getString("BUSINESSSTATUSNAME"));
                paramsMap.put("BUTTONNAME", signMaxLog.getString("BUTTONNAME"));
                paramsMap.put("BUTTONURL", signMaxLog.getString("BUTTONURL"));
                paramsMap.put("BUTTONTYPE", signMaxLog.getString("BUTTONTYPE"));
                /*signMaxLog.set("SENDREFERENCE", paramsMap.get("SENDREFERENCE"));
                signMaxLog.set("PARTICIPATION", paramsMap.get("PARTICIPATION"));*/
                String insertState = middleLogUtil.InsertBtnLog(paramsMap);
                operation = insertState;

            } else {
                signMaxLog.set("APPLICATIONNAME", paramsMap.get("APPLICATIONNAME"));
                signMaxLog.set("TASK_INSTID", paramsMap.get("TASKINSTID"));
                signMaxLog.set("PROCESS_INSTID", paramsMap.get("PROCESSINSTID"));
                signMaxLog.set("PROCESS_DEFID", paramsMap.get("PROCESSDEFID"));
                signMaxLog.set("BUSINESSID", paramsMap.get("BUSINESSID"));
                signMaxLog.set("BUSINESSNAME", paramsMap.get("BUSINESSNAME"));
                signMaxLog.set("BUSINESSSTATUSID", paramsMap.get("BUSINESSSTATUSID"));
                signMaxLog.set("BUSINESSSTATUSNAME", paramsMap.get("BUSINESSSTATUSNAME"));
                signMaxLog.set("BUTTONID", paramsMap.get("BUTTONID"));
                signMaxLog.set("BUTTONNAME", paramsMap.get("BUTTONNAME"));
                signMaxLog.set("BUTTONURL", paramsMap.get("BUTTONURL"));
                signMaxLog.set("BUTTONTYPE", paramsMap.get("BUTTONTYPE"));
                signMaxLog.set("BPMBUTTONID", paramsMap.get("BPMBUTTONID"));
                signMaxLog.set("BPMBUTTONNAME", paramsMap.get("BPMBUTTONID"));
                signMaxLog.set("SENDREFERENCE", paramsMap.get("SENDREFERENCE"));
                signMaxLog.set("PARTICIPATION", paramsMap.get("PARTICIPATION"));
                signMaxLog.set("BUTTONSTATUS", paramsMap.get("BUTTONSTATUS"));
                signMaxLog.set("BUSINESSRESULT", paramsMap.get("BUSINESSRESULT"));
                signMaxLog.set("BUSINESSSTATUSTIME", paramsMap.get("BUSINESSSTATUSTIME"));
                signMaxLog.set("NOTES", paramsMap.get("NOTES"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date nowDateTime = new Date(System.currentTimeMillis());
                String nowDateTimeFormat = sdf.format(nowDateTime);
                signMaxLog.set("RECEIVERESULTS", nowDateTimeFormat);
                int recordCount = SDK.getBOAPI().update("BO_PS_MIDDLE_OPERATION_LOG", signMaxLog);
                try {
                    recordCount = SDK.getBOAPI().update("BO_PS_MIDDLE_OPERATION_LOG",signMaxLog);
                    operation = "updateOK";
                } catch (Exception e) {
                    operation = "updateERROP";
                }
            }
        } else {
            operation = "updateNULL";
            System.out.println("更新失败："+operation);
        }
        System.out.println("执行更新操作完成");
        return operation;
    }
}
